package com.suyx.job.service.sect.impl;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Optional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.suyx.job.config.RedisUtils;
import com.suyx.job.controller.MhxyHundrSmTaskController;
import com.suyx.job.entity.RoleInfo;
import com.suyx.job.enums.SectEnums;
import com.suyx.job.service.sect.AbsGoToService;
import com.suyx.job.utils.DeviceCodeMapUtil;
import com.suyx.job.utils.ImagePHash;
import com.suyx.job.utils.ImageUtils;
import com.suyx.job.utils.ScreenCapture;
import com.suyx.job.utils.SpeechUtil;
import com.suyx.job.utils.SysStaticData;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Service
public class GoToTgSectServiceImpl extends AbsGoToService {
	@Autowired
	private RedisUtils redisService;
	
	@Override
	public boolean getTask(RoleInfo roleInfo, String path, String taskType) {
		try {
			// 已经在=在师傅处了，原地领取任务
			if ("0".equals(taskType)) {
				// 点击师门任务追踪
				ScreenCapture.mouseClick(roleInfo.getDevice(), "1430 287");
				Thread.sleep(2 * 1000L);
				// 去掉对话框
				ScreenCapture.mouseClick(roleInfo.getDevice(), "670 589");
			}

			// 先回到门派再在师傅处领取任务
			if ("1".equals(taskType)) {
				goBackHome(roleInfo, path);
				// 点击师门任务追踪
				ScreenCapture.mouseClick(roleInfo.getDevice(), "1430 287");
				Thread.sleep(23 * 1000L);
				// 去掉对话框
				ScreenCapture.mouseClick(roleInfo.getDevice(), "670 589");
			}
		} catch (Exception e) {

		}

		return false;
	}

	@Override
	public boolean finishTask(RoleInfo roleInfo, String path, String taskType) {
		try {
			goBackHome(roleInfo, path);
			// 点击师门任务
			ScreenCapture.mouseClick(roleInfo.getDevice(), "1430 287");
			Thread.sleep(23 * 1000L);
			if("1".equals(taskType)){//上交物品
				ScreenCapture.mouseClick(roleInfo.getDevice(), "456 643");
			}
			
			// 去掉对话框
			ScreenCapture.mouseClick(roleInfo.getDevice(), "670 589");
		} catch (Exception e) {
		}
		return false;
	}

	@Override
	public boolean support(RoleInfo roleInfo) {
		return SectEnums.TG.getCode().equals(roleInfo.getRoleSect());
	}

	@Override
	public void goBackHome(RoleInfo roleInfo, String path) {
		// =======================回门派========================
		String device = roleInfo.getDevice();
		while (true) {
			Integer sectNum = (Integer) Optional.ofNullable(redisService.get(roleInfo.getRoleName())).orElse(0);
			if(sectNum >= 20){
				log.info(roleInfo.getRoleName() + "今天已经完成了20次师门任务");
				break;
			}
			
			ScreenCapture.snapshot(device, path, "screen.png");
			try {
				ImageUtils.cutPNG(new FileInputStream(path + "screen.png"),
						new FileOutputStream(path + device + ".png"), 120, 48, 63, 36);
			} catch (IOException e) {
				e.printStackTrace();
			}
			ImagePHash p = new ImagePHash();
			try {
				String image1 = p.getHash(new FileInputStream(new File(path + device + ".png")));
				String image2 = p
						.getHash(new FileInputStream(new File(SysStaticData.SC_PIC_PATH + "tg_is_arrive_sm.png")));

				int score = p.distance(image1, image2);
				log.info(roleInfo.getRoleName() + "是否在回tg师门点：" + score);
				if (score > 10) {
					// ScreenCapture.mouseClick(device, "1400 40");//
					// ScreenCapture.mouseClick(device, "1193 109");// # 加血
					// ScreenCapture.mouseClick(device, "1400 40");//
					// ScreenCapture.mouseClick(device, "1359 109");// # 使用

					ScreenCapture.mouseClick(device, "1211 682");
					ScreenCapture.mouseClick(device, "1264 618");
					ScreenCapture.mouseClick(device, "1211 682");
				} else {
					break;
				}
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		// =====================回门派=====================
	}

	public boolean execute1(RoleInfo roleInfo, String path, String taskType) {
		try {
			// 点击地图到ps门口
			ScreenCapture.mouseClick(roleInfo.getDevice(), "148 52");
			ScreenCapture.mouseClick(roleInfo.getDevice(), "1073 228");
			// 关闭地图
			ScreenCapture.mouseClick(roleInfo.getDevice(), "1130 81");
			Thread.sleep(9 * 1000L);
			ScreenCapture.mouseClick(roleInfo.getDevice(), "1259 104");
			Thread.sleep(2 * 1000L);
			ScreenCapture.mouseClick(roleInfo.getDevice(), "492 38");
			Thread.sleep(3 * 1000L);
			ScreenCapture.mouseClick(roleInfo.getDevice(), "821 30");
			Thread.sleep(2 * 1000L);
			ScreenCapture.mouseClick(roleInfo.getDevice(), "787 155");
			// 领取任务
			if ("1".equals(taskType)) {
				ScreenCapture.mouseClick(roleInfo.getDevice(), "1248 223");
				Thread.sleep(1 * 1000L);
				ScreenCapture.mouseClick(roleInfo.getDevice(), "670 589");
			}
			// 交任务并领取任务
			if ("2".equals(taskType)) {
				ScreenCapture.mouseClick(roleInfo.getDevice(), "1200 322");
				// 确认提交物品
				ScreenCapture.mouseClick(roleInfo.getDevice(), "461 645");
				// 去掉对话框
				Thread.sleep(1 * 1000L);
				ScreenCapture.mouseClick(roleInfo.getDevice(), "670 589");
				Thread.sleep(1 * 1000L);
				ScreenCapture.mouseClick(roleInfo.getDevice(), "787 155");
				Thread.sleep(1 * 1000L);
				ScreenCapture.mouseClick(roleInfo.getDevice(), "1248 223");
				Thread.sleep(1 * 1000L);
				ScreenCapture.mouseClick(roleInfo.getDevice(), "670 589");
			}
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		return false;
	}

	@Override
	public void userfaShu(RoleInfo roleInfo, String path) {
		//ScreenCapture.mouseClick(roleInfo.getDevice(), "1387 264");
		// 使用
		if("yes".equals(roleInfo.getIsLingbao())){
			ScreenCapture.mouseClick(roleInfo.getDevice(), "1291 344");
		}else{
			ScreenCapture.mouseClick(roleInfo.getDevice(), "1288 431");
		}
		//使用随机目标
		ScreenCapture.mouseClick(roleInfo.getDevice(), "1385 573");
		//自动
		ScreenCapture.mouseClick(roleInfo.getDevice(), "1390 681"); 
	}

	@Override
	public void userfaShuForA(RoleInfo roleInfo, String path) {
		//ScreenCapture.mouseClick(roleInfo.getDevice(), "1387 264");
		// 使用
		if("yes".equals(roleInfo.getIsLingbao())){
			ScreenCapture.mouseClick(roleInfo.getDevice(), "1291 344");
		}else{
			ScreenCapture.mouseClick(roleInfo.getDevice(), "1288 431");
		}
		//使用随机目标
		ScreenCapture.mouseClick(roleInfo.getDevice(), "1385 573");
		ScreenCapture.mouseClick(roleInfo.getDevice(), "1390 681"); 
	}
}
